package business_logic.view;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import fiuba.algo3.titiritero.dibujables.SuperficiePanel;
import fiuba.algo3.titiritero.modelo.SuperficieDeDibujo;

public class TextView implements GameView
{
    private static final String DEFAULT_FONT_NAME = Font.SANS_SERIF;
    private static final int    DEFAUL_FONT_SIZE  = 96;
    private static final int    DEFAUL_FONT_STYLE = Font.BOLD;
    private static final Color  DEFAULT_COLOR     = Color.WHITE;
    
    private String m_fontName  = DEFAULT_FONT_NAME;
    private int    m_fontSize  = DEFAUL_FONT_SIZE;
    private int    m_fontStyle = DEFAUL_FONT_STYLE;
    private Color m_color      = DEFAULT_COLOR;
    private int m_xPosition;
    private int m_yPosition;
    private Font m_font;
    private int m_zIndex = 10000;    // Ver si esto vale la pena hacerlo configurable
    private String m_text;
    private boolean m_centerHorizontally;
    private boolean m_centerVertically;
    private float m_transparency = 1.0f;
    
    
    public TextView( String text )
    {
        m_text = text;
        m_font = new Font( m_fontName, m_fontStyle, m_fontSize);
        m_centerHorizontally = true;
        m_centerVertically = true;
    }
    
    protected void setText( String text )
    {
        m_text = text;
        m_font = new Font( m_fontName, m_fontStyle, m_fontSize);
    }
    protected String getText()
    {
        return m_text;
    }
    
    public void setTransparency( float transparency )
    {
        m_transparency = transparency;
    }
    
    protected void setFont( Font font )
    {
        m_fontName  = font.getName();
        m_fontSize  = font.getSize();
        m_fontStyle = font.getStyle();
        m_font = new Font( m_fontName, m_fontStyle, m_fontSize);
    }
    
    protected void setSize( int size )
    {
        m_fontSize = size;
        m_font = new Font( m_fontName, m_fontStyle, m_fontSize);
    }
    
    protected void setStyle( int style )
    {
        m_fontStyle = style;
        m_font = new Font( m_fontName, m_fontStyle, m_fontSize);
    }
    
    // Ojo! Fixear la posición hace q el texto no esté más centrado!!
    protected void setPosition( int x, int y )
    {
        m_xPosition = x;
        m_yPosition = y;
        m_centerHorizontally = false;
        m_centerVertically = false;
    }
    
    // Ojo! Fixear la posición X hace q el texto no esté más centrado Horizontalmente !!
    protected void setXPosition( int x )
    {
        m_xPosition = x;
        m_centerHorizontally = false;
    }
    
    // Ojo! Fixear la posición Y hace q el texto no esté más centrado Verticalmente!!
    protected void setYPosition( int y )
    {
        m_xPosition = y;
        m_centerVertically = false;
    }
    
    // OJO! centrar sobre-escribe la posición (x,y) anterior.
    protected void center()
    {
        m_centerHorizontally = true;
        m_centerVertically = true;
    }
    
    // OJO! Centrar Horizontalmente sobre-escribe la posición en X.
    protected void centerHorizontally()
    {
        m_centerHorizontally = true;
    }
    
    // OJO! Centrar Verticalmente sobre-escribe la posición en Y.
    protected void centerVertically()
    {
        m_centerHorizontally = true;
        m_centerVertically = true;
    }
    
    protected void setColor ( Color color )
    {
        m_color = color;
    }
    
    private void centerTextIn( SuperficieDeDibujo graphicSurface )
    {
        SuperficiePanel panel = ((SuperficiePanel)graphicSurface);
        
        Graphics2D g2 = (Graphics2D)panel.getBuffer();
        
        FontMetrics fm   = g2.getFontMetrics( m_font );
        java.awt.geom.Rectangle2D rect = fm.getStringBounds( m_text, g2 );

        if ( m_centerHorizontally )
        {
            int textWidth   = (int)(rect.getWidth());
            int panelWidth  = (int) panel.getWidth();
            m_xPosition = (panelWidth  - textWidth)  / 2;
        }
        
        if ( m_centerVertically )
        {
            int textHeight  = (int)(rect.getHeight());
            int panelHeight = (int) panel.getHeight();
            m_yPosition = (panelHeight - textHeight) / 2  + fm.getAscent();
        }
        
    }
    
    @Override
    public void dibujar( SuperficieDeDibujo graphicSurface )
    {   
        Graphics2D g2 = (Graphics2D)((SuperficiePanel)graphicSurface).getBuffer();
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
            RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2.setFont( m_font );
        g2.setColor( m_color );
        
        Composite c = AlphaComposite.getInstance(AlphaComposite.SRC_OVER,m_transparency);
        g2.setComposite(c);
        
        if ( m_centerHorizontally )
            this.centerTextIn(graphicSurface);

        g2.drawString( m_text, m_xPosition, m_yPosition );  // Draw the string.
    }

    @Override
    public boolean isDone()
    {
        return true;
    }

    @Override
    public int getZIndex()
    {
        return m_zIndex;
    }

}
